'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户认证
 * @author sundbcn@gmail.com (sun debing)
 */

var config_util = require('../util/configUtil');
var http_util = require('kc-http');

/**
 * 验证signed_key是否有效
 * @param  {String}   signed_key 登录账号的signed_key
 * @param  {Function} cb         回调
 * @return {Void}
 */
exports.checkSignedKey = function (signed_key, cb) {
    var auth_config = config_util.load('auth');

    var onGet = function (err, data) {
        /*jslint evil: true */
        var json_request = (new Function('return (' + data + ')')());
        if (json_request.code !== 10000) {
            cb(new Error('Verification fail'));
            return;
        }
        cb(null, json_request.result.uid, json_request.result.channel);
    };

    var url = _getCheckUrl(auth_config.host, signed_key);
    http_util.get(url, onGet);
};

/**
 * 获取验证的地址
 * @param  {String} base_path  地址ip
 * @param  {String} signed_key 验证串
 * @param  {Int}    game_id    游戏id
 * @return {[type]}
 */
var _getCheckUrl = function (base_path, signed_key) {
    return base_path + '?' + 'signed_key=' + signed_key;
};
